<?php
/**
 * 用户相关的操作（登录，注册，修改密码，信息修改） 
 * @author:zhulin
 * @since:2012 5.24
 */
class UserController extends BaseController{
    
    public  function register(){
        //账户信心
        $username = $this->getParamString('username');
        $password = $this->getParamString('password');
        $email = $this->getParamString('email');
        $headPicInfo = $_FILES['headPic'];
        
        if(! empty($headPicInfo)){
        // 上传头像
        $path = 'file/uploads/headImg';
        $PicThumb = User_Info::imgUpload($headPicInfo, $path , 100 ,100); 
            
        }
        
        
        //学员信息
        $sex = empty($_POST['sex'])?NULL:$this->getParamInt('sex');
        $mobile = empty( $_POST['mobile'])?NULL: $this->getParamInt('mobile');
        $birth = empty ($_POST['birth'])?NULL:$this->getParamString('birth');
        $addr = empty($_POST['addr'])?NULL:$this->getParamString('address');
        
        //邮箱发送验证注册账户
         $result = Helper_PhpMailer::getInstance()->sendEmail($email);
         
         if( $result ==1 ){
             
        //注册账户信息 
        $userInfo = User_Info::userRegister( $username , $password , $email , $PicThumb );
      
        //注册学员信息
        $uid = mysql_insert_id();
        $registerTime  = strtotime(date('Y-m-d h:i:s') );
        $usereInfoExtend = User_Info::userRegisterExtend(  $uid , $sex , $mobile , $birth , $addr , $registerTime );
            
         Helper_Url::redirect('index.php');
        
         }
       
        
    }
    /**
     * 用户登录接口
     * @param string Uname     用户名
     * @param string Password  密码
     * @return array
     * <pre>
     *  array(
     *      UId:int
     *      UName:string
     *      Password:string
     *  )
     * </pre>
     */
   public function login(){
       
        $username = $this->getParamString('username');
        $password = $this->getParamString('password');
        
        //密码加密
        $key = Common::getConfig('passEncrypt');
        $passwdHash = Helper_String::password_hash($password, $key);

        
        $userInfo = User_Info::getUserInfo($username, $passwdHash);
      
        if (empty($userInfo)) {
            throw new AppException(3001);
        }
       
        
        $_SESSION['name'] = $userInfo['name'];
        $_SESSION['uId'] = $userInfo['uId'];

         //修改学员扩展表
        $userExtendInfo = User_Info::getInstance()->userExtendList( $_SESSION['uId']);
        $lastLogin = strtotime(date('Y-m-d h:i:s') );
        $totalDays = round (($lastLogin - $userExtendInfo['registerTime'])/3600/24) ;
        $userExtend = User_Info::getInstance()->updateUserExtend( array('lastLogin' => $lastLogin , 'totalDays' => $totalDays) );
        Helper_Url::redirect('index.php');
       
   }
   
    /**
     * 退出登录，清空用户的设备信息+2.
     * @return array
     * <pre>
     *  array(
     *      userInfo:UserInfo_Info
     *  )
     * </pre>
     */
    public function loginout()
    { 

        unset($_SESSION['uId']);
        unset($_SESSION['name']);
        Helper_Url::redirect('index.php');
    }
    /**
     * 用户密码修改
     * 
     * @param string    $oldPasswd
     * @param string    $newPasswd
     * @return array
     * <pre>
     *  array(
     *      userInfo:UserInfo_Info
     *      sessionId:string
     *  )
     * </pre>
     */
    public function changePasswd()
    {
        $oldPasswd = $this->getParamString( 'oldPasswd' );
        $newPasswd = $this->getParamString( 'newPasswd' );
        $uId = $_SESSION['uId'];
        $userModel = User_Info::getinstance( $uId );
       
        //获取老密码hash值
       $passwdHash = $userModel->getInfoUserPasswd( $uId );   
        //验证原始密码输入是否正确
        $key = Common::getConfig( 'passEncrypt' );
        $oldPasswdHash = Helper_String::password_hash( $oldPasswd , $key );
        if( $oldPasswdHash != $passwdHash )
        {
            throw new AppException( 3001 );
        }
        
        //更新密码
        
        $userInfo = $userModel->updateUserPass( array( 'passwd' => $newPasswd ) );
        unset($_SESSION['uId']);
        unset($_SESSION['name']);
        Helper_Url::redirect('index.php');

    }
    /**
     * 会员资料修改
     */
    public function changeData(){
        $email = $this->getParamString('email', true , true );
        $headPic = empty( $_FILES['headPic'])?$this->getParamString('oldPic'):$_FILES['headPic'];
        $sex = $this->getParamInt( 'sex');
        $mobile = $this->getParamInt( 'mobile');
        $birth = $this->getParamInt('birth');
        $addr = $this->getParamString('addr' ,true ,true );
       
         if ($headPic['error'] !== 0 || !is_uploaded_file($headPic['tmp_name'])) {
            throw new AppException(2003);
        }
        $path= "file/uploads/headImg";                            //缩略图图片
                        //源图

        /**
         * 上传图片
         */
        if( !empty( $headPic)){
             $newPic = User_Info::getInstance()->imgUpload($headPic, $path ,100 , 100 );
        }
         $userBaseInfo = array(
            'email' =>$email,
            'headPic'=>$newPic  
        );
        $usereExtendInfo = array(    
             'sex'=>$sex,
            'mobile'=>$mobile,
            'birth'=>$birth,
            'addr'=>$addr
        );
        //添加到数据库
        $userUpdate = User_Info::getInstance()->updateUserInfo( $userBaseInfo ,$usereExtendInfo );
         include TPL_DIR.'member.php';
        
        
    }

    
}
